package com.lyf.scm.common.util.style;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;


/**
 * excel单元格样式工具类
 *
 * @author wangchlei
 * @date 2020/08/19
 */
public class ExcelCellStyleHandler {
    private final Workbook workbook;

    public ExcelCellStyleHandler(Workbook workbook) {
        this.workbook = workbook;
    }

    /**
     * 设置高级搜索标题样式
     *
     * @param
     * @return {@link CellStyle}
     **/
    public CellStyle getAdvancedSearchHeadCellStyle() {
        XSSFCellStyle xSSFCellStyle = this.getxSSFCellStyle();
        if (xSSFCellStyle == null) {
            return null;
        }
        this.generalCellStyle(xSSFCellStyle);
        //设置单元格的水平对齐方式——居左
        xSSFCellStyle.setAlignment(HorizontalAlignment.LEFT);
        //设置前景色填充方式
        xSSFCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置填充颜色
        xSSFCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(248, 248, 248)));
        //设置字体
        Font headFont = workbook.createFont();
        //加粗
        headFont.setBold(true);
        //字体大小
        headFont.setFontHeightInPoints((short) (14));
        headFont.setFontName("微软雅黑");
        xSSFCellStyle.setFont(headFont);
        return xSSFCellStyle;
    }

    /**
     * 设置日期标题样式
     *
     * @return {@link CellStyle}
     **/
    public CellStyle getDateHeadCellStyle() {
        XSSFCellStyle xSSFCellStyle = this.getxSSFCellStyle();
        if (xSSFCellStyle == null) {
            return null;
        }
        this.generalCellStyle(xSSFCellStyle);
        //设置单元格的水平对齐方式——居右
        xSSFCellStyle.setAlignment(HorizontalAlignment.RIGHT);
        //设置前景色填充方式
        xSSFCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置填充颜色
        xSSFCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(75, 162, 253)));
        //设置字体(白色)
        Font headFont = workbook.createFont();
        //加粗
        headFont.setBold(true);
        //字体大小
        headFont.setFontHeightInPoints((short) (12));
        headFont.setFontName("宋体");
        headFont.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        //字体类型
        xSSFCellStyle.setFont(headFont);
        return xSSFCellStyle;
    }

    /**
     * 获取样式实例
     *
     * @param
     * @return {@link XSSFCellStyle}
     **/
    private XSSFCellStyle getxSSFCellStyle() {
        CellStyle advancedSearchHeadCellStyle = this.workbook.createCellStyle();
        XSSFCellStyle xSSFCellStyle = null;
        if (advancedSearchHeadCellStyle instanceof XSSFCellStyle) {
            xSSFCellStyle = (XSSFCellStyle) advancedSearchHeadCellStyle;
        }
        return xSSFCellStyle;
    }

    /**
     * 设置单元格通用样式
     *
     * @param cellStyle
     * @return void
     **/
    private void generalCellStyle(CellStyle cellStyle) {
        //设置是否应该换行
        cellStyle.setWrapText(false);
        //设置单元格的垂直对齐方式——居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.GREY_80_PERCENT.getIndex());
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.GREY_80_PERCENT.getIndex());
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.GREY_80_PERCENT.getIndex());
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.GREY_80_PERCENT.getIndex());
    }

    /**
     * 内容体通用样式(居右)
     *
     * @param
     * @return {@link CellStyle}
     **/
    public CellStyle getBodyGeneralCellStyleRight() {
        XSSFCellStyle xSSFCellStyle = this.getxSSFCellStyle();
        if (xSSFCellStyle == null) {
            return null;
        }
        this.generalCellStyle(xSSFCellStyle);
        //设置单元格的水平对齐方式——居左
        xSSFCellStyle.setAlignment(HorizontalAlignment.RIGHT);
        //设置字体
        Font headFont = workbook.createFont();
        //字体大小
        headFont.setFontName("宋体");
        xSSFCellStyle.setFont(headFont);
        return xSSFCellStyle;
    }
    /**
     * 内容体通用样式(居左)
     *
     * @param
     * @return {@link CellStyle}
     **/
    public CellStyle getBodyGeneralCellStyleLeft() {
        XSSFCellStyle xSSFCellStyle = this.getxSSFCellStyle();
        if (xSSFCellStyle == null) {
            return null;
        }
        this.generalCellStyle(xSSFCellStyle);
        //设置单元格的水平对齐方式——居左
        xSSFCellStyle.setAlignment(HorizontalAlignment.LEFT);
        //设置字体
        Font headFont = workbook.createFont();
        //字体大小
        headFont.setFontName("微软雅黑");
        xSSFCellStyle.setFont(headFont);
        return xSSFCellStyle;
    }

    public CellStyle getPercentageCellStyle() {
        CellStyle cellStyle = getBodyGeneralCellStyleRight();
        cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.000%"));
        return cellStyle;
    }
}
